﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class InCameraCtrl : MonoBehaviour
{
	[Header("配置")]
	public bool if_debug; //是否测试，显示出感应的debug文字
	public int in_x1 = 700;
	public int in_x2 = 1200;

	[Header("内部对象")]
	public GameObject log_obj;
	public Text log;

	[Header("回调函数")]
	public UnityEvent OnLeft;
	public UnityEvent OnRight;

	private bool is_cal = false;
	private bool is_cal_left = false;
	private bool is_cal_stop = false;

	private bool is_left = false;
	private bool is_center = false;
	private bool is_right = false;

	private int debug_left = 0;
	private int debug_center = 0;
	private int debug_right = 0;

	//静止检查
	private Vector3 mouse_pos;
	private Vector3 old_mouse_pos;
	private bool is_move = false;

	private void Start()
	{
		if (if_debug)
		{
			log_obj.SetActive(true);
		}
		else
		{
			log_obj.SetActive(false);
		}

		//InvokeRepeating("IfStop", 2, 2);

		InvokeRepeating("InputCal", 0.1f, 0.1f);
	}

	//private void Update()
	//{
	//	InputCal();
	//}

	public void InputCal()
	{
		if (is_cal_stop) return;

		Vector3	pos = Input.mousePosition;

		if (pos.x <= in_x1)
		{
			if (is_cal)
			{
				if (if_debug)
				{
					debug_left++;
					SetLog();
				}

				//计算中
				if (!is_center)
				{
					is_cal_left = true;
					is_left = true;
				}
				else
				{
					is_left = true;
					InputRun();
				}
				
			}
			else
			{
				if (if_debug)
				{
					debug_left++;
					SetLog();
				}

				//开始计算
				is_cal = true;
				is_cal_left = true;

				is_left = true;
			}

		}
		else if (pos.x > in_x1 && pos.x < in_x2)
		{
			if (is_cal)
			{
				if (if_debug)
				{
					debug_center++;
					SetLog();
				}

				is_center = true;
				InputRun();
			}

		}
		else if (pos.x >= in_x2)
		{
			if (is_cal)
			{
				if (if_debug)
				{
					debug_right++;
					SetLog();
				}

				//计算中
				if (!is_center)
				{
					is_cal_left = false;
					is_right = true;
				}
				else
				{
					is_right = true;
					InputRun();
				}
				
			}
			else
			{
				if (if_debug)
				{
					debug_right++;
					SetLog();
				}

				//开始计算
				is_cal = true;
				is_cal_left = false;

				is_right = true;
			}

		}
	}

	private void InputRun()
	{
		if (is_left && is_right && is_center)
		{
			is_cal_stop = true;
			Invoke("InputReSet", 1f);

			is_left = false;
			is_right = false;
			is_center = false;

			if (is_cal_left)
			{
				if (OnLeft.GetPersistentEventCount() > 0)
				{
					OnLeft.Invoke();
				}
			}
			else
			{
				if (OnRight.GetPersistentEventCount() > 0)
				{
					OnRight.Invoke();
				}
			}

			is_cal = false;

		}
	}

	private void InputReSet()
	{
		is_cal_stop = false;

		if (if_debug)
		{
			log.text = "";
		}
		
	}

	private void SetLog()
	{
		log.text = "left:" + debug_left.ToString() + ",center:" + debug_center.ToString() + ",right:" + debug_right.ToString();
	}

	private void IfStop()
	{
		old_mouse_pos = mouse_pos;
		mouse_pos = Input.mousePosition;

		if (old_mouse_pos == mouse_pos)
		{
			log.text = "静止";

			is_left = false;
			is_right = false;
			is_center = false;

			is_cal_stop = false;
			is_cal = false;
		}
	}
}
